/*
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-08-23 15:27:24
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2025-02-27 13:49:40
 * @FilePath: \global-brain\global-brain-admin\src\utils\lee.ts
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved.
 */

/**
 * 字典通过value获取label
 * @param dict 字典
 * @param type 字典类型
 * @param value 值
 * @returns
 */
export const getDictLabel = (dict, type, value) => {
  // console.log(dict, type, value)
  if (!dict[type]) {
    alert(type + '字典不存在')
    return
  }
  const data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

/**
 * 转换成直接可以查询权限的结构
 * @param data 权限数据
 * @param codeField 代码字段名称
 * @returns JSON键值对,{code:authority}
 */
export const changeAuthority = (data, codeField = 'code') => {
  const newData = {}
  data.forEach((item) => {
    newData[item[codeField]] = item.authority
  })
  return newData
}

/**
 * 转化路由
 * @param menuData 菜单数据
 * @param menuAuthority 菜单权限（如果没传这个值就代表全部菜单转化成路由）
 * @returns 路由JSON结构
 */
export const changeRoute = (menuData, menuAuthority = []) => {
  const newData: any = []
  menuData.forEach((item) => {
    const newItem: any = {
      path: item.path,
      component: item.component,
      name: item.menu_code,
      meta: {
        title: item.menu_name,
        hidden: item.hidden == '1' ? true : false,
        alwaysShow: item.alwaysShow == '1' ? true : false,
        noCache: item.noCache == '1' ? true : false,
        breadcrumb: item.breadcrumb == '1' ? true : false,
        affix: item.affix == '1' ? true : false,
        noTagsView: item.noTagsView == '1' ? true : false,
        canTo: item.canTo == '1' ? true : false
      }
    }
    if (item.redirect != null && item.redirect != '') {
      newItem.redirect = item.redirect
    }
    if (item.icon != null && item.icon != '') {
      newItem.meta.icon = item.icon
    }
    if (item.activeMenu != null && item.activeMenu != '') {
      newItem.meta.activeMenu = item.activeMenu
    }
    if (item.followAuth != null && item.followAuth != '') {
      newItem.meta.followAuth = item.followAuth
    }

    if (item.children != null && item.children != '') {
      newItem.children = changeRoute(item.children, menuAuthority)
      if (newItem.children.length > 0) {
        newData.push(newItem)
      }
    } else {
      if (menuAuthority.length == 0 || menuAuthority[item.menu_code] == 1) {
        newData.push(newItem)
      }
    }
  })
  return newData
}

/**
 * 转换应用结构
 * @param appData 菜单数据
 * @param appAuthority 菜单权限 (未转化的权限结构，需要转化一次)
 * @returns 菜单JSON
 */
export const changeApp = (appData, appAuthority) => {
  const newAppAuthority = changeAuthority(appAuthority, 'app_id')
  const newData: any = []
  appData.forEach((item) => {
    if (newAppAuthority[item.app_id] == 1) {
      newData.push(item)
    }
  })
  return newData
}

/**
 * 转换菜单结构
 * @param menuData 菜单数据
 * @param menuAuthority 菜单权限 (未转化的权限结构，需要转化一次)
 * @returns 菜单JSON
 */
export const changeMenu = (menuData, menuAuthority) => {
  const newMenuAuthority = changeAuthority(menuAuthority, 'menu_code')
  const newData: any = []
  menuData.forEach((item) => {
    const newItem: any = JSON.parse(JSON.stringify(item))
    if (item.children != null && item.children != '') {
      newItem.children = changeMenu(item.children, menuAuthority)
      if (newItem.children.length > 0) {
        newData.push(newItem)
      }
    } else {
      if (newMenuAuthority[item.menu_code] == 1) {
        newData.push(newItem)
      }
    }
  })
  return newData
}

/**
 * 转换功能结构
 * @param faData 功能数据
 * @param faAuthority 功能权限 (未转化的权限结构，需要转化一次)
 * @returns 功能JSON
 */
export const changeFa = (faData, faAuthority) => {
  const newFaAuthority = changeAuthority(faAuthority, 'fa_code')
  const newData: any = []
  faData.forEach((item) => {
    const newItem: any = JSON.parse(JSON.stringify(item))
    if (item.children != null && item.children != '') {
      newItem.children = changeFa(item.children, faAuthority)
      if (newItem.children.length > 0) {
        newData.push(newItem)
      }
    } else {
      if (newFaAuthority[item.fa_code] == 1) {
        newData.push(newItem)
      }
    }
  })
  return newData
}

const options = [
  {
    value: '1',
    label: '没有权限'
  },
  {
    value: '2',
    label: '本人'
  },
  {
    value: '3',
    label: '本部门'
  },
  {
    value: '4',
    label: '本级及下级部门'
  },
  {
    value: '5',
    label: '本组织'
  },
  {
    value: '6',
    label: '全部'
  }
]

/**
 * 转换数据权限结构
 * @param daData 数据权限数据
 * @param daAuthority 数据权限 (未转化的权限结构，需要转化一次)
 * @returns 功能JSON
 */
export const changeDa = (daData, daAuthority = null) => {
  let newDaAuthority: any = {}
  if (daAuthority == null) {
    newDaAuthority = changeAuthority([], 'da_code')
  } else {
    newDaAuthority = changeAuthority(daAuthority, 'da_code')
  }
  const newData: any = []
  daData.forEach((item) => {
    const newItem: any = JSON.parse(JSON.stringify(item))
    if (item.children != null && item.children != '') {
      newItem.children = changeDa(item.children, daAuthority)
      if (newItem.children.length > 0) {
        if (daAuthority == null) {
          newItem.options = options
          newData.push(newItem)
        } else {
          if (newDaAuthority[item.da_code] > 1) {
            newItem.options = options.slice(0, newDaAuthority[item.da_code])
          } else {
            newItem.options = options.slice(0, 1)
          }
          newData.push(newItem)
        }
      }
    } else {
      if (daAuthority == null) {
        newItem.options = options
        newData.push(newItem)
      } else {
        if (newDaAuthority[item.da_code] > 1) {
          newItem.options = options.slice(0, newDaAuthority[item.da_code])
          newData.push(newItem)
        }
      }
    }
  })
  return newData
}

/**
 * 通过权限转换ids
 * @param authority 权限数据
 * @param idField id字段
 * @returns 数组 [1,2,4]
 */
export const changeIds = (authority, idField = 'id') => {
  const newData: any = []
  authority.forEach((item) => {
    if (item.authority == 1) {
      newData.push(item[idField])
    }
  })
  return newData
}
